/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package similarite;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Clement
 */
public class Normalisation extends Thread {

    private int norme;
    private Scanner scan;
    private SommeVecteur sV;
    private File file;
    private BufferedWriter out;
    private boolean running;
    private Writer fstream;

    public Normalisation(File file, SommeVecteur sV) {
        this.sV = sV;
        this.norme = -1;
        this.running = false;
        this.file = file;
        try {
            this.scan = new Scanner(file);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Normalisation.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public Normalisation(String s, SommeVecteur sV) {
        this.sV = sV;
        this.norme = -1;
        this.running = false;
        this.scan = new Scanner(s);
    }

    public boolean isRunning() {
        return running;
    }

    public void setRunning(boolean running) {
        this.running = running;
    }

    public int getNorme() {
        return norme;
    }

    public synchronized void setNorme(int norme) {
        this.norme = norme;
    }

    public Writer getFstream() {
        return fstream;
    }

    public void setFstream(Writer fstream) {
        this.fstream = fstream;
    }

    @Override
    public void run() {
        try {
            normLine();
            sV.getNewNorme();
        } catch (IOException ex) {
            Logger.getLogger(Normalisation.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    synchronized private void normLine() throws IOException {
        if (file != null) {
            for (int j = 0; j < 5; j++) {
                scan.next();
            }
        }

        for (int i = 0; i < sV.getnCol(); i++) {
            String s = scan.next();
//            System.out.print(s+", ");
            Float n = Float.parseFloat(s);
            if(sV.getnCol() == 128) {
                n = n / norme * 1000;
            } else {
                n = n / norme;
            }
            out.write(n + " ");
        }
        out.write("\n");
    }

    public void initFile() {
        try {

            if (file != null) {
                fstream = new FileWriter("src/norm/" + file.getName() + ".norm");
            } else {
                fstream = new StringWriter();
            }
            out = new BufferedWriter(fstream);



        } catch (IOException ex) {
            Logger.getLogger(Normalisation.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void initImage() {
        initFile();
        for (int i = 0; i < 2; i++) {
            scan.next();
        }
    }

    public void endFile() {
        try {
            out.close();


        } catch (IOException ex) {
            Logger.getLogger(Normalisation.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}
